package Store;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import SimpleDB.DatabaseSetUp;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.simpledb.AmazonSimpleDB;
import com.amazonaws.services.simpledb.AmazonSimpleDBClient;
import com.amazonaws.services.simpledb.model.Attribute;
import com.amazonaws.services.simpledb.model.BatchPutAttributesRequest;
import com.amazonaws.services.simpledb.model.CreateDomainRequest;
import com.amazonaws.services.simpledb.model.DeleteAttributesRequest;
import com.amazonaws.services.simpledb.model.DeleteDomainRequest;
import com.amazonaws.services.simpledb.model.Item;
import com.amazonaws.services.simpledb.model.PutAttributesRequest;
import com.amazonaws.services.simpledb.model.ReplaceableAttribute;
import com.amazonaws.services.simpledb.model.ReplaceableItem;
import com.amazonaws.services.simpledb.model.SelectRequest;

@SuppressWarnings("unused")
public class DatabaseAccess {
	AmazonSimpleDB sdb;
	
	public DatabaseAccess(){
		try{
			connectToTheDatabase();
		}
		catch(Exception E){
			System.out.println("Could not connect to database!");
			System.out.println("Please contact customer care!");
		}
	}
	
	public static void main(String... Args){
		DatabaseAccess databaseAccess = new DatabaseAccess();
		databaseAccess.listDomains();
		
	}
	
	public void listDomains(){
        System.out.println("Listing all domains in your account:\n");
        for (String domainName : this.sdb.listDomains().getDomainNames()) {
            System.out.println("  " + domainName);
        }
        System.out.println();
	}
	
	public void connectToTheDatabase() throws Exception{
	  	System.out.println("===========================================");
        System.out.println("Connecting to Amazon SimpleDB");
        System.out.println("===========================================\n");
        sdb = new AmazonSimpleDBClient(new PropertiesCredentials(
        DatabaseSetUp.class.getResourceAsStream("AwsCredentials.properties")));
	  	System.out.println("===========================================");
        System.out.println("Connected to Amazon SimpleDB");
        System.out.println("===========================================\n");
	}
	
	public boolean addItemToDomain(Account... accounts) throws Exception {
		String myDomain = "Accounts";
		sdb.batchPutAttributes(new BatchPutAttributesRequest(myDomain, createListAccounts(accounts)));
		return true;
	}
	
	private List<ReplaceableItem> createListAccounts(Account... accounts) {
		List<ReplaceableItem> accountData = new ArrayList<ReplaceableItem>();
		for(Account a : accounts){
			accountData.add(new ReplaceableItem(a.getAccountID()).withAttributes(
					new ReplaceableAttribute("AccountID", a.getAccountID(), true),
	                new ReplaceableAttribute("Email", a.getEmail(), true),
	                new ReplaceableAttribute("HashPassword", a.getHashPassword(), true),
	                new ReplaceableAttribute("SecurityQuestion1", a.getSecurityQuestion1(), true),
	                new ReplaceableAttribute("SecurityQuestion2", a.getSecurityQuestion2(), true),
	                new ReplaceableAttribute("SecurityAnswer1", a.getSecurityAnswer1(), true),
	                new ReplaceableAttribute("SecurityAnswer2", a.getSecurityAnswer2(), true),
	                new ReplaceableAttribute("Date", a.getLastLogin().toString(), true),
	                new ReplaceableAttribute("Balance", a.getBalance().toString(), true),
	                new ReplaceableAttribute("StoreUser", a.isNonUser().toString(), true),
	                new ReplaceableAttribute("CustomerID", a.getCustomerID(), true)));
			try {
				this.addItemToDomain(a.getCustomer());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return accountData;
	}
	
	public boolean addItemToDomain(Address... addresses) throws Exception {
		String myDomain = "Adresses";
		sdb.batchPutAttributes(new BatchPutAttributesRequest(myDomain, createListAdresses(addresses)));
		return true;
	}
	
	private List<ReplaceableItem> createListAdresses(Address[] addresses) {
		List<ReplaceableItem> addressesData = new ArrayList<ReplaceableItem>();
		for(Address a : addresses){
			addressesData.add(new ReplaceableItem(a.getAddressID()).withAttributes(
	                new ReplaceableAttribute("AddressID", a.getAddressID(), true),
	                new ReplaceableAttribute("AccountID", a.getAccountID(), true),
	                new ReplaceableAttribute("CustomerID", a.getCustomerID(), true),
	                new ReplaceableAttribute("OrderID", a.getOrderID(), true),
	                new ReplaceableAttribute("Street1", a.getStreet1(), true),
	                new ReplaceableAttribute("Street2", a.getStreet2(), true),
	                new ReplaceableAttribute("City", a.getCity(), true),
	                new ReplaceableAttribute("State", a.getState(), true),
	                new ReplaceableAttribute("Zip", a.getZip(), true),
	                new ReplaceableAttribute("Country", a.getCountry(), true),
	                new ReplaceableAttribute("Type", a.getType(), true)));
		}
		return addressesData;
	}

	public boolean addItemToDomain(Customer... customers) throws Exception {
		String myDomain = "Customers";
		sdb.batchPutAttributes(new BatchPutAttributesRequest(myDomain, createListCustomers(customers)));
		return true;
	}
	
	private List<ReplaceableItem> createListCustomers(Customer[] customers) {
		List<ReplaceableItem> customersData = new ArrayList<ReplaceableItem>();
		for(Customer a : customers){
			customersData.add(new ReplaceableItem(a.getCustomerID()).withAttributes(
	                new ReplaceableAttribute("AccountID", a.getAccountID(), true),
	                new ReplaceableAttribute("CustomerID", a.getCustomerID(), true),
	                new ReplaceableAttribute("CustomerAdrID", a.getCustomerAdrID(), true),
	                new ReplaceableAttribute("FirstName", a.getFirstName(), true),
	                new ReplaceableAttribute("LastName", a.getLastName(), true),
	                new ReplaceableAttribute("Phone", a.getPhone(), true)));
			try {
				this.addItemToDomain(a.getCustomerAdr());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return customersData;
	}

	public boolean addItemToDomain(Store.Import... importInstance) throws Exception {
		String myDomain = "ImportHistory";
		sdb.batchPutAttributes(new BatchPutAttributesRequest(myDomain, createListImports(importInstance)));
		return true;
	}
	
	private List<ReplaceableItem> createListImports(Import... importInstances) {
		List<ReplaceableItem> importData = new ArrayList<ReplaceableItem>();
		for(Import a : importInstances){
			Date date = new Date();
			importData.add(new ReplaceableItem(a.getImportID()).withAttributes(
	                new ReplaceableAttribute("ImportID", a.getImportID(), true),
	                new ReplaceableAttribute("AddedItems", a.getAddedItems().toString(), true),
	                new ReplaceableAttribute("DeletedItems", a.getdeletedItems().toString(), true),
	                new ReplaceableAttribute("SaleStartedItems", a.getSaleStartItems().toString(), true),
	                new ReplaceableAttribute("SaleEndedItems", a.getSaleEndedItems().toString(), true),
	                new ReplaceableAttribute("DateStamp", date.toString(), true)));
			this.addItemsToDomain(a.getChangeList().getArray());
		}
		return importData;
	}

	public boolean addItemToDomain(Order... orders) throws Exception {
		String myDomain = "Orders";
		sdb.batchPutAttributes(new BatchPutAttributesRequest(myDomain, createListOrders(orders)));
		return true;
	}
	
	private List<ReplaceableItem> createListOrders(Order[] orders) {
		List<ReplaceableItem> orderData = new ArrayList<ReplaceableItem>();
		for(Order a : orders){
			orderData.add(new ReplaceableItem(a.getOrderID()).withAttributes(
	                new ReplaceableAttribute("OrderID", a.getOrderID(), true),
	                new ReplaceableAttribute("CustomerID", a.getCustomerID(), true),
	                new ReplaceableAttribute("BillingAddressID", a.getBillingAdrID(), true),
	                new ReplaceableAttribute("ShippingAddressID", a.getShippingAdrID(), true),
	                new ReplaceableAttribute("ShipDate", a.getShipDate().toString(), true),
	                new ReplaceableAttribute("OrderTotal", a.getTotal().toString(), true),
	                new ReplaceableAttribute("OrderTax", a.getTax().toString(), true),
	                new ReplaceableAttribute("OrderSubTotal", a.getSubtotal().toString(), true),
	                new ReplaceableAttribute("OrderStatus", a.getStatus(), true),
	                new ReplaceableAttribute("DiscountPercent", a.getDiscountPercent().toString(), true),
	                new ReplaceableAttribute("DiscountAmount", a.getDiscountAmount().toString(), true),
	                new ReplaceableAttribute("SameAdr", a.getSameBillShipAdr().toString(), true),
	                new ReplaceableAttribute("OrderDate", a.getOrderDate().toString(), true)));
		}
		return orderData;
	}

	public boolean addItemToDomain(Payment... payments) throws Exception {
		String myDomain = "Payments";
		sdb.batchPutAttributes(new BatchPutAttributesRequest(myDomain, createListPayments(payments)));
		return true;
	}
	
	private List<ReplaceableItem> createListPayments(Payment[] payments) {
		List<ReplaceableItem> paymentsData = new ArrayList<ReplaceableItem>();
		for(Payment a : payments){
			paymentsData.add(new ReplaceableItem(a.getPaymentID()).withAttributes(
	                new ReplaceableAttribute("PaymentID", a.getPaymentID(), true),
	                new ReplaceableAttribute("OrderID", a.getOrderID(), true),
	                new ReplaceableAttribute("CardName", a.getCardName(), true),
	                new ReplaceableAttribute("CardType", a.getCardType(), true),
	                new ReplaceableAttribute("CardNumber", a.getCardNumber(), true),
	                new ReplaceableAttribute("EXPMonth", a.getExpMonth().toString(), true),
	                new ReplaceableAttribute("EXPYear", a.getExpyYear().toString(), true),
	                new ReplaceableAttribute("SecurityCode", a.getSecurityCode().toString(), true),
	                new ReplaceableAttribute("BillingAdrID", a.getBillingAdrID(), true)));
		}
		return paymentsData;
	}

	public boolean addItemsToDomain(Store.Item... items)throws Exception {
		System.out.println("I was hwere... ");
		String myDomain = "Items";
		sdb.batchPutAttributes(new BatchPutAttributesRequest(myDomain, createListItems(items)));
		return true;
	}

	private List<ReplaceableItem> createListItems(Store.Item... items) {
		System.out.println("I was hwere..11111111. ");
		List<ReplaceableItem> itemsData = new ArrayList<ReplaceableItem>();
		for(Store.Item a : items){
			System.out.println("ahhhhhhhhhh. ");
			itemsData.add(new ReplaceableItem(a.getItemID()).withAttributes(
	                new ReplaceableAttribute("ItemID", a.getItemID().toString(), true),
	                new ReplaceableAttribute("Name", a.getName().toString(), true),
	                new ReplaceableAttribute("ISBN", a.getISBN().toString(), true),
	                new ReplaceableAttribute("Description", a.getDescription().toString(), true),
	                new ReplaceableAttribute("Price", a.getPrice().toString(), true),
	                new ReplaceableAttribute("OnSale", a.isOnSale().toString(), true),
	                new ReplaceableAttribute("SalePrice", a.getSalePrice().toString(), true),
	                new ReplaceableAttribute("Quantity", a.getQuantity().toString(), true),
	                new ReplaceableAttribute("Available", a.isAvailable().toString(), true),
	                new ReplaceableAttribute("Tags", a.getTags().toString(), true)));
		}
		System.out.println("I was hwere..22222222. ");
		return itemsData;
	}
	
	public boolean addItemsToDomain(ItemChange... itemsChanged) {
		String myDomain = "ItemChangeHistory";
		sdb.batchPutAttributes(new BatchPutAttributesRequest(myDomain, createListItemsChanged(itemsChanged)));
		return true;
	}

	@SuppressWarnings("deprecation")
	private List<ReplaceableItem> createListItemsChanged(ItemChange[] itemsChanged) {
		Date date = new Date();
		List<ReplaceableItem> itemsData = new ArrayList<ReplaceableItem>();
		for(ItemChange a : itemsChanged){
			itemsData.add(new ReplaceableItem(a.getItemChangeID()).withAttributes(
	                new ReplaceableAttribute("ItemID", a.getItemID().toString(), true),
	                new ReplaceableAttribute("ItemChangeID", a.getItemChangeID().toString(), true),
	                new ReplaceableAttribute("TypeOfChange", a.getTypeOfChange().toString(), true),
	                new ReplaceableAttribute("UpdateTime", date.toGMTString(), true)));
		}
		return itemsData;
	}

	public void deleteItem(Store.Item... item) {
		// TODO Auto-generated method stub
		
	}

	public void changeItemToDomain(Store.Item item) throws Exception {
		deleteItem(item);
		addItemsToDomain(item);
		
	}

	@SuppressWarnings("deprecation")
	public Account[] checkUserNamePasswordAndGetAccount(String email,
			String hashPassword) {
		
		String selectExpression = "select * from `" + "Accounts" + "` where Email = '" + email + "'";
        System.out.println("Selecting: " + selectExpression + "\n");
        SelectRequest selectRequest = new SelectRequest(selectExpression);
        Integer numberofItems = sdb.select(selectRequest).getItems().size();
        int count = 0;
        Account returnAccounts[] = new Account[numberofItems];
        for (Item item : sdb.select(selectRequest).getItems()) {
        	Account constructAccount = new Account();
        	for (Attribute attribute : item.getAttributes()) {
                if(attribute.getName().toString().equals("AccountID")){
                	constructAccount.setAccountID(attribute.getValue());
                }
				else if(attribute.getName().toString().equals("CustomerID")){
					constructAccount.setCustomerID(attribute.getValue());               	
				 }
				else if(attribute.getName().toString().equals("Email")){
					constructAccount.setEmail(attribute.getValue());
				}
				else if(attribute.getName().toString().equals("HashPassword")){
					constructAccount.setHashPassword(attribute.getValue());
				}
				else if(attribute.getName().toString().equals("SecurityQuestion1")){
					constructAccount.setSecurityQuestion1(attribute.getValue());
				}
				else if(attribute.getName().toString().equals("SecurityQuestion2")){
					constructAccount.setSecurityQuestion2(attribute.getValue());
				}
				else if(attribute.getName().toString().equals("SecurityAnswer1")){
					constructAccount.setSecurityAnswer1(attribute.getValue());
				}
				else if(attribute.getName().toString().equals("SecurityAnswer2")){
					constructAccount.setSecurityAnswer2(attribute.getValue());
				}
				else if(attribute.getName().toString().equals("Date")){
					constructAccount.setLastLogin(new Date(attribute.getValue()));
				}
				else if(attribute.getName().toString().equals("Balance")){
					constructAccount.setBalance(Float.parseFloat(attribute.getValue()));
				}
				else if(attribute.getName().toString().equals("StoreUser")){
					constructAccount.setNonUser(Boolean.parseBoolean(attribute.getValue()));
				}
            }
            returnAccounts[count] = constructAccount;
            count++;
        }
        return returnAccounts;
	}

	public Address[] getAddress(String adrID) {
		String selectExpression = "select * from `" + "Addresses" + "` where AddressID = '" + adrID + "'";
        System.out.println("Selecting: " + selectExpression + "\n");
        SelectRequest selectRequest = new SelectRequest(selectExpression);
        Integer numberofItems = sdb.select(selectRequest).getItems().size();
        int count = 0;
        Address returnAddresses[] = new Address[numberofItems];
        for (Item item : sdb.select(selectRequest).getItems()) {
        	Address constructAddress = new Address();
        	for (Attribute attribute : item.getAttributes()) {
                if(attribute.getName().toString().equals("AddressID")){
                	constructAddress.setAddressID(attribute.getValue());
                }
                else if(attribute.getName().toString().equals("AccountID")){
                	constructAddress.setAccountID(attribute.getValue());
                }
				else if(attribute.getName().toString().equals("CustomerID")){
					constructAddress.setCustomerID(attribute.getValue());               	
				 }
				else if(attribute.getName().toString().equals("OrderID")){
					constructAddress.setOrderID(attribute.getValue());
				}
				else if(attribute.getName().toString().equals("Street1")){
					constructAddress.setStreet1(attribute.getValue());
				}
				else if(attribute.getName().toString().equals("Street2")){
					constructAddress.setStreet2(attribute.getValue());
				}
				else if(attribute.getName().toString().equals("City")){
					constructAddress.setCity(attribute.getValue());
				}
				else if(attribute.getName().toString().equals("State")){
					constructAddress.setState(attribute.getValue());
				}
				else if(attribute.getName().toString().equals("Zip")){
					constructAddress.setZip(attribute.getValue());
				}
				else if(attribute.getName().toString().equals("Country")){
					constructAddress.setCountry(attribute.getValue());
				}
				else if(attribute.getName().toString().equals("Type")){
					constructAddress.setType(attribute.getValue());
				}
            }
            returnAddresses[count] = constructAddress;
            count++;
        }
        return returnAddresses;
	}

	public Customer[] getCustomer(String customerID) {
		String selectExpression = "select * from `" + "Customers" + "` where CustomerID = '" + customerID + "'";
        System.out.println("Selecting: " + selectExpression + "\n");
        SelectRequest selectRequest = new SelectRequest(selectExpression);
        Integer numberofItems = sdb.select(selectRequest).getItems().size();
        int count = 0;
        Customer returnCustomers[] = new Customer[numberofItems];
        for (Item item : sdb.select(selectRequest).getItems()) {
        	Customer constructCustomer = new Customer();
        	for (Attribute attribute : item.getAttributes()) {
                if(attribute.getName().toString().equals("AccountID")){
                	constructCustomer.setAccountID(attribute.getValue());
                }
                else if(attribute.getName().toString().equals("CustomerID")){
                	constructCustomer.setCustomerID(attribute.getValue());
                }
				else if(attribute.getName().toString().equals("CustomerAdrID")){
					constructCustomer.setCustomerAdrID(attribute.getValue());               	
				 }
				else if(attribute.getName().toString().equals("FirstName")){
					constructCustomer.setFirstName(attribute.getValue());
				}
				else if(attribute.getName().toString().equals("LastName")){
					constructCustomer.setLastName(attribute.getValue());
				}
				else if(attribute.getName().toString().equals("Phone")){
					constructCustomer.setPhone(attribute.getValue());
				}
            }
            returnCustomers[count] = constructCustomer;
            count++;
        }
        return returnCustomers;
	}

	public Store.Item[] getItem(String itemID) {
		String selectExpression = "select * from `" + "Items" + "` where ItemID = '" + itemID + "'";
        System.out.println("Selecting: " + selectExpression + "\n");
        SelectRequest selectRequest = new SelectRequest(selectExpression);
        Integer numberofItems = sdb.select(selectRequest).getItems().size();
        int count = 0;
        Store.Item returnItems[] = new Store.Item[numberofItems];
        for (Item item : sdb.select(selectRequest).getItems()) {
        	Store.Item constructItem = new Store.Item();
        	for (Attribute attribute : item.getAttributes()) {
                if(attribute.getName().toString().equals("ItemID")){
                	constructItem.setItemID(attribute.getValue());
                }
                else if(attribute.getName().toString().equals("Name")){
                	constructItem.setName(attribute.getValue());
                }
				else if(attribute.getName().toString().equals("ISBN")){
					constructItem.setISBN(attribute.getValue());               	
				 }
				else if(attribute.getName().toString().equals("Description")){
					constructItem.setDescription(attribute.getValue());
				}
				else if(attribute.getName().toString().equals("Price")){
					constructItem.setPrice(Float.parseFloat(attribute.getValue()));
				}
				else if(attribute.getName().toString().equals("OnSale")){
					constructItem.setOnSale(Boolean.parseBoolean(attribute.getValue()));
				}
				else if(attribute.getName().toString().equals("SalePrice")){
					constructItem.setSalePrice(Float.parseFloat(attribute.getValue()));
				}
				else if(attribute.getName().toString().equals("Quantity")){
					constructItem.setQuantity(Integer.parseInt(attribute.getValue()));
				}
				else if(attribute.getName().toString().equals("Available")){
					constructItem.setAvailable(Boolean.parseBoolean(attribute.getValue()));
				}
				else if(attribute.getName().toString().equals("Tags")){
					// TODO.... 
					//String temp = attribute.getValue();
					//String temp1[] = 
					//constructItem.setTags(attribute.getValue());
				}

            }
            returnItems[count] = constructItem;
            count++;
        }
        return returnItems;
	}

	@SuppressWarnings({ "deprecation"})
	public Order[] getOrder(String orderID) {
		String selectExpression = "select * from `" + "Orders" + "` where OrderID = '" + orderID + "'";
        System.out.println("Selecting: " + selectExpression + "\n");
        SelectRequest selectRequest = new SelectRequest(selectExpression);
        Integer numberofItems = sdb.select(selectRequest).getItems().size();
        int count = 0;
        Order returnOrders[] = new Order[numberofItems];
        for (Item item : sdb.select(selectRequest).getItems()) {
        	Order constructOrder = new Order();
        	for (Attribute attribute : item.getAttributes()) {
                if(attribute.getName().toString().equals("OrderID")){
                	constructOrder.setOrderID(attribute.getValue());
                }
                else if(attribute.getName().toString().equals("CustomerID")){
                	constructOrder.setCustomerID(attribute.getValue());
                }
				else if(attribute.getName().toString().equals("BillingAddressID")){
					constructOrder.setBillingAdrID(attribute.getValue());               	
				 }
				else if(attribute.getName().toString().equals("ShippingAddressID")){
					constructOrder.setShippingAdrID(attribute.getValue());
				}
				else if(attribute.getName().toString().equals("ShipDate")){
					constructOrder.setShipDate(new Date(attribute.getValue()));
				}
				else if(attribute.getName().toString().equals("OrderTotal")){
					constructOrder.setTotal(Float.parseFloat(attribute.getValue()));
				}
				else if(attribute.getName().toString().equals("OrderTax")){
					constructOrder.setTax(Float.parseFloat(attribute.getValue()));
				}
				else if(attribute.getName().toString().equals("OrderSubTotal")){
					constructOrder.setSubtotal(Float.parseFloat(attribute.getValue()));
				}
				else if(attribute.getName().toString().equals("OrderStatus")){
					constructOrder.setStatus(attribute.getValue());
				}
				else if(attribute.getName().toString().equals("DiscountPercent")){
					constructOrder.setDiscountPercent(Float.parseFloat(attribute.getValue()));
				}
				else if(attribute.getName().toString().equals("DiscountAmount")){
					constructOrder.setDiscountAmount(Float.parseFloat(attribute.getValue()));
				}
				else if(attribute.getName().toString().equals("SameAdr")){
					constructOrder.setSameBillShipAdr(Boolean.parseBoolean(attribute.getValue()));
				}
				else if(attribute.getName().toString().equals("OrderDate")){
					constructOrder.setOrderDate(new Date(attribute.getValue()));
				}
            }
            returnOrders[count] = constructOrder;
            count++;
        }
        return returnOrders;
	}

	public Payment[] getPayment(String PaymentID) {
		String selectExpression = "select * from `" + "Payments" + "` where PaymentID = '" + PaymentID + "'";
        System.out.println("Selecting: " + selectExpression + "\n");
        SelectRequest selectRequest = new SelectRequest(selectExpression);
        Integer numberofItems = sdb.select(selectRequest).getItems().size();
        int count = 0;
        Payment returnPayments[] = new Payment[numberofItems];
        for (Item item : sdb.select(selectRequest).getItems()) {
        	Payment constructPayment = new Payment();
        	for (Attribute attribute : item.getAttributes()) {
                if(attribute.getName().toString().equals("PaymentID")){
                	constructPayment.setPaymentID(attribute.getValue());
                }
                else if(attribute.getName().toString().equals("OrderID")){
                	constructPayment.setOrderID(attribute.getValue());
                }
				else if(attribute.getName().toString().equals("CardName")){
					constructPayment.setCardName(attribute.getValue());               	
				 }
				else if(attribute.getName().toString().equals("CardType")){
					constructPayment.setCardType(attribute.getValue());
				}
				else if(attribute.getName().toString().equals("CardNumber")){
					constructPayment.setCardNumber(attribute.getValue());
				}
				else if(attribute.getName().toString().equals("EXPMonth")){
					constructPayment.setExpMonth(Integer.parseInt(attribute.getValue()));
				}
				else if(attribute.getName().toString().equals("EXPYear")){
					constructPayment.setExpyYear(Integer.parseInt(attribute.getValue()));
				}
				else if(attribute.getName().toString().equals("SecurityCode")){
					constructPayment.setSecurityCode(Integer.parseInt(attribute.getValue()));
				}
				else if(attribute.getName().toString().equals("BillingAdrID")){
					constructPayment.setBillingAdrID(attribute.getValue());
				}
            }
            returnPayments[count] = constructPayment;
            count++;
        }
        return returnPayments;
	}
}